package com.zzusedating.androidapp;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.zzusedating.androidapp.customlayout.TitleBarLayout;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.BasicHandlerCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class LeaveMsgActivity extends BaseActivity {

    private EditText editText;
    private int userId;

    private BasicCallback callback;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_msg_layout);

        userId = getIntent().getIntExtra("userId", -1);
        if (userId == -1){
            MyApplication.Toast(ConstantsTools.NETWORK_ERROR_);
            return;
        }

        initCallback();
        editText = findViewById(R.id.LeaveMsg_edit);
        TitleBarLayout titleBarLayout = findViewById(R.id.LeaveMsg_title);
        titleBarLayout.setClickListenerTo(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable s = editText.getText();
                if (TextUtils.isEmpty(s))
                    return;
                MyApplication.getClient().newCall(
                        new Request.Builder()
                                .get()
                                .url(String.format(ConstantsTools.LEAVE_MESSAGE, userId, s.toString()))
                                .build()
                ).enqueue(callback);
            }
        }, TitleBarLayout.TO_TEXT);
    }

    @Override
    protected void initCallback() {
        handler = new Handler(new BasicHandlerCallback(){
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what){
                    case ConstantsTools.NETWORK_ERROR_I:
                        MyApplication.Toast(ConstantsTools.NETWORK_ERROR_);
                        break;
                    case -1:
                        MyApplication.Toast("未登录");
                        break;
                    case 5:
                        MyApplication.Toast("用户不存在");
                        break;
                    case 1:
                        setResult(RESULT_OK);
                        finish();
                }
                return true;
            }
        });
        callback = new BasicCallback(handler) {
            @Override
            public void onResponse(Call call, @NonNull Response response) {
                handler.sendMessage(ConstantsTools.createMessage(getStatus(response)));
            }
        };
    }
}
